Проблема: стандартные массивы для очереди могут привести к необходимости дорогостоящих операций сдвига элементов при удалении.
Решение: в книге Algorithms and Data Structures for OOP With C# автор предлагает реализовать очередь на основе связного списка, что позволяет эффективно добавлять элементы в конец и удалять с начала за O(1).
Пример кода:
public class Node<T> { public T Data; public Node<T> Next;
public Node(T data) { Data = data; Next = null; } }
public class QueueLinkedList<T> { private Node<T> front, rear;
public QueueLinkedList() { front = rear = null; }
public void Enqueue(T item) { var newNode = new Node<T>(item); if (rear == null) { front = rear = newNode; return; } rear.Next = newNode; rear = newNode; }
public T Dequeue() { if (front == null) throw new InvalidOperationException("Queue is empty.");
var data = front.Data; front = front.Next;
if (front == null) rear = null;
return data; } }
Преимущества: — Нет затрат на сдвиг элементов — Высокая производительность при операциях добавления и удаления — Универсальная реализация для любых типов данных
Проблема: стандартные массивы для очереди могут привести к необходимости дорогостоящих операций сдвига элементов при удалении.
Решение: в книге Algorithms and Data Structures for OOP With C# автор предлагает реализовать очередь на основе связного списка, что позволяет эффективно добавлять элементы в конец и удалять с начала за O(1).
Пример кода:
public class Node<T> { public T Data; public Node<T> Next;
public Node(T data) { Data = data; Next = null; } }
public class QueueLinkedList<T> { private Node<T> front, rear;
public QueueLinkedList() { front = rear = null; }
public void Enqueue(T item) { var newNode = new Node<T>(item); if (rear == null) { front = rear = newNode; return; } rear.Next = newNode; rear = newNode; }
public T Dequeue() { if (front == null) throw new InvalidOperationException("Queue is empty.");
var data = front.Data; front = front.Next;
if (front == null) rear = null;
return data; } }
Преимущества: — Нет затрат на сдвиг элементов — Высокая производительность при операциях добавления и удаления — Универсальная реализация для любых типов данных
Telegram today rolling out an update which brings with it several new features.The update also adds interactive emoji. When you send one of the select animated emoji in chat, you can now tap on it to initiate a full screen animation. The update also adds interactive emoji. When you send one of the select animated emoji in chat, you can now tap on it to initiate a full screen animation. This is then visible to you or anyone else who's also present in chat at the moment. The animations are also accompanied by vibrations. This is then visible to you or anyone else who's also present in chat at the moment. The animations are also accompanied by vibrations.
Telegram is riding high, adding tens of million of users this year. Now the bill is coming due.Telegram is one of the few significant social-media challengers to Facebook Inc., FB -1.90% on a trajectory toward one billion users active each month by the end of 2022, up from roughly 550 million today.